package com.yy.invoice.utils;

import com.baidu.aip.contentcensor.EImgType;
import com.baidu.aip.ocr.AipOcr;
import com.yy.invoice.entity.Invoice;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.util.HashMap;

@Slf4j
public class BaiDuApiUtils {

    public static Invoice vatInvoice(String path,String appId,String apiKey,String secretKey){
        AipOcr client = new AipOcr(appId,apiKey,secretKey);
        JSONObject jsonObject = client.vatInvoice(path, EImgType.PDF, new HashMap<>());
        log.info("识别返回结果为：{}",jsonObject);
        String validate = jsonObject.getJSONObject("words_result").getString("CheckCode");
        Invoice invoice = new Invoice(
                null,
                null,
                jsonObject.getJSONObject("words_result").getString("InvoiceCode"),
                jsonObject.getJSONObject("words_result").getString("InvoiceDate").replace("年","-").replace("月","-").replace("日",""),
                jsonObject.getJSONObject("words_result").getString("InvoiceTypeOrg"),
                jsonObject.getJSONObject("words_result").getString("InvoiceNumConfirm"),
                jsonObject.getJSONObject("words_result").getString("AmountInFiguers"),
                validate.substring(validate.length() - 6 ),
                null,
                null,
                null
        );
        return invoice;
    }
}
